﻿# coding:utf-8
from bs4 import BeautifulSoup

class HtmlParser(object):
	def __init__(self):
		pass
	
	def home_page_msg(self,content):
		category = self.get_catgory_name(content)
		page_num = self.get_page_num(content)
		return category,page_num
	
	def get_catgory_name(self,content):
		soup = BeautifulSoup(content)
		h1 = soup.find('h1',class_='crumb-h1')
		return h1.get_text()
	
	def get_page_num(self,content):
		soup = BeautifulSoup(content)
		a_list = soup.find_all('a', class_='page-item ')
		a = a_list[len(a_list)-1]
		return int(a.get_text())
	
	def detail_page_url(self,content):
		soup = BeautifulSoup(content)
		a_list = soup.find_all('a', class_='name')
		detail_urls = list()
		for a in a_list:
			detail_urls.append(a['href'])
		return detail_urls
	
	# 大小       app_size     <meta itemprop="fileSize">
	# 分类       category_list  <dd class='tag-box'>
	# 版本       app_version   <dd>
	# 系统       app_system   <dd class='perms'>
	# 来源       app_source  <span itemprop="name">
	# 更新时间   app_time    <time itemprop="datePublished">
	def app_detail_msg(self,content):
		soup = BeautifulSoup(content)
		d = dict()
		d['app_name'] = soup.find('span',class_='title').get_text()
		d['app_size'] = soup.find('meta',itemprop='fileSize')['content']
		d['category_list'] = soup.find('dd',class_='tag-box').get_text().strip()
		d['app_version'] = soup.find_all('dd')[3].get_text()
		d['app_system'] = soup.find('dd',class_='perms').next_element.strip()
		d['app_source'] = soup.find_all('dd')[5].get_text().strip()
		d['app_time'] = soup.find('time', itemprop='datePublished').get_text()
		return d
		
	